package com.example.flow.flow.service;

import com.example.flow.dao.FlowTaskDao;
import com.example.flow.dao.FlowTaskHisDao;
import com.example.flow.dao.model.FlowTask;
import com.example.flow.dao.model.FlowTaskHis;
import com.example.flow.flow.FlowConstant;
import com.example.flow.flow.core.Execution;
import com.example.flow.flow.model.ProcessModel;
import com.example.flow.flow.model.TaskModel;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * 流程任务
 *
 * @author tangaq
 * 2023/6/15
 */
@Service
public class FlowTaskService {
    private static final Logger log = LoggerFactory.getLogger(FlowTaskService.class);

    private final FlowTaskDao taskDao;
    private final FlowTaskHisDao taskHisDao;

    public FlowTaskService(FlowTaskDao taskDao,
                           FlowTaskHisDao historyTaskDao) {
        this.taskDao = taskDao;
        this.taskHisDao = historyTaskDao;
    }

    public void createTask(TaskModel model, Execution execution) {
        FlowTask task = new FlowTask();
        task.setTaskId(UUID.randomUUID().toString().replace("-", ""));
        task.setOrderId(execution.getOrder().getOrderId());
        task.setTaskName(model.getName());
        task.setParentTaskId(execution.getTask() == null ? FlowConstant.NODE_START : execution.getTask().getTaskId());
        task.setParentTaskName(execution.getTask() == null ? FlowConstant.NODE_START : execution.getTask().getTaskName());
        String operator = (String) execution.getArgs().get(FlowConstant.CURRENT_OPERATOR_GUID);
        task.setOperator(operator);
        taskDao.insert(task);
    }

    public FlowTask findByTaskId(String taskId) {
        return taskDao.findByTaskId(taskId);
    }

    /**
     * 完成任务
     */
    public FlowTask complete(String taskId) {
        FlowTask task = this.findByTaskId(taskId);

        FlowTaskHis history = new FlowTaskHis(task);
        history.setHisTaskId(UUID.randomUUID().toString().replace("-", ""));
        history.setFinishTime("");
        history.setTaskState(FlowConstant.STATE_FINISH);
        taskHisDao.insert(history);

        taskDao.deleteTask(taskId);
        log.info("complete task:[{}]", taskId);
        return task;
    }

    public FlowTask findByOrderId(String orderId) {
        return taskDao.findByOrderId(orderId);
    }

    /**
     * 驳回
     *
     * @return
     */
    public FlowTask rejectTask(ProcessModel model, FlowTask currentTask) {
        String parentTaskId = currentTask.getParentTaskId();
        if (StringUtils.isBlank(parentTaskId) || FlowConstant.NODE_START.equals(parentTaskId)) {
            throw new RuntimeException("上一步任务ID为空，无法驳回至上一步处理");
        }
        FlowTaskHis hisTask = taskHisDao.findByTaskId(parentTaskId);
        FlowTask task = hisTask.getRedoTask();
        task.setTaskId(UUID.randomUUID().toString().replace("-", ""));
        task.setCts(System.currentTimeMillis());
        taskDao.insert(task);
        return task;
    }

    public void deleteByOrderId(String orderId) {
        taskDao.deleteByOrderId(orderId);
        taskHisDao.deleteByOrderId(orderId);
    }
}
